<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController 
{
	//管理员列表
    public function index()
    {
        $admins = M('admins')->select();
        $this->assign('admins',$admins);

        $this->display();
    }

    //添加显示
    public function add_index()
    {
        $this->display();
    }
    //添加
    public function add()
    {
        //[username] => aaaaaa[pass] => aaaaaa[repass] => aaaaaa
        if (IS_POST) {
            $data['username'] = I('post.username');
            $pass = I('post.pass');
            $repass = I('post.repass');

            $username = M('admins')->where(array('username'=>$data['username']))->find();
            if ($username) {
                $arr = array(
                    'error' => 0,
                    'info' => '账号已存在'
                     );
                echo json_encode($arr);
                exit;
            }
            //密码
            if ($pass != $repass) {
                $arr = array(
                    'error' => 0,
                    'info' => '两次密码不一样'
                     );
                echo json_encode($arr);
                exit;
            }

            //入库
            $data['password'] = md5($pass);
            $data['create_time'] = time();
            $data['create_ip'] = get_client_ip();

            $res = M('admins')->add($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '添加成功'
                     );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '添加失败'
                     );
                echo json_encode($arr);
            }

        }
    }

    //用户停用 交互
    public function user_stop()
    {
       //print_r($_POST);exit;
      if (IS_POST) {
        $userid = I('post.userid');
        $sql = 'update admins set is_dongjie=0 where id='.$userid;
        $is_dongjie = M()->execute($sql);
        if ($is_dongjie) {
            $arr = array(
                'error' =>0,
                'info'=>"用户停用成功",
                'userid' =>$userid
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>1,
                'info'=>"用户停用失败"
                );
            echo json_encode($arr);
        }

      }
        
    }
    //用户启用 交互
    public function user_start()
    {//print_r($_POST);exit;
      if (IS_POST) {
          $userid = I('post.userid');
          $sql = 'update admins set is_dongjie=1 where id='.$userid;
          $is_dongjie = M()->execute($sql);
          if ($is_dongjie) {
              $arr = array(
                  'error' =>0,
                  'info'=>"用户启用成功",
                  'userid' =>$userid
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"用户启用失败"
                  );
              echo json_encode($arr);
          }
      }
    }

    //编辑显示
    public function edit_index()
    {
        $id = I('get.id');

        $admin = M('admins')->where(array('id'=>$id))->find();
        $this->assign('admin',$admin);

        $this->display();
    }
    //编辑显示
    public function edit()
    {
        if (IS_POST) {
            $id = I('post.id');

            $data['username'] = I('post.username');
            $pass = I('post.pass');
            $repass = I('post.repass');

            //密码
            if ($pass != $repass) {
                $arr = array(
                    'error' => 0,
                    'info' => '两次密码不一样'
                     );
                echo json_encode($arr);
                exit;
            }

            //更新
            $res = M('admins')->where(array('id'=>$id))->save($data);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '更新成功'
                     );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '更新失败'
                     );
                echo json_encode($arr);
            }
        }
    }

    //删除
    public function is_del()
    {
        if (IS_POST) {
            $id = I('post.id');

            $res = M('admins')->delete($id);
            if ($res) {
                $arr = array(
                    'error' => 1,
                    'info' => '删除成功'
                     );
                echo json_encode($arr);
            }else {
                $arr = array(
                    'error' => 0,
                    'info' => '删除失败'
                     );
                echo json_encode($arr);
            }
        }
    }















}